﻿@using Webdiyer.WebControls.Mvc
@model PagedList<ContosoUniversity.Models.Student>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm())
{ 
 <p>

Find By Name:@Html.TextBox("searchString", ViewBag.Filter as string) &nbsp;
     <input type="submit" value="Search" />
 </p>
}
<p>共有 @Model.TotalItemCount 条记录 @Model.CurrentPageIndex/@Model.TotalItemCount</p>
@Html.Pager(Model, new PagerOptions
{
    PageIndexParameterName = "page",
    ShowPageIndexBox = true,
    PageIndexBoxType =
        PageIndexBoxType.TextBox,
    PageIndexBoxWrapperFormatString = "请输入页数{0}"},
        "Default",
        new
        {
            sortOrder = ViewBag.DateSortParm,
            searchString = ViewBag.Filter
})

<table>
    <tr>
        <th>
            @*@Html.DisplayNameFor(model => model.LastName)*@
            @Html.ActionLink("Last Name", "Index", new {sortOrder=ViewBag.NameSortParm })
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstMidName)
        </th>
        <th>
            @*@Html.DisplayNameFor(model => model.EnrollmentDate)*@
            @Html.ActionLink("EnrollmentDate", "Index", new { sortOrder = ViewBag.DateSortParm })
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FirstMidName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EnrollmentDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.StudentID }) |
            @Html.ActionLink("Details", "Details", new { id=item.StudentID }) |
            @*@Html.ActionLink("Delete", "Delete", new { id=item.StudentID })*@
             <a  name="Delete" stuid="@item.StudentID">Delete</a>
        </td>
    </tr>
}

</table>
@section scripts{
@{Html.RegisterMvcPagerScriptResource();}
<script type="text/javascript">
$(function () {
        $("[name='Delete']").click(
        function () {
            if (confirm("确定要删除这条记录么？")) {
                var stuid = $(this).attr("stuid");
                var tr = $(this).parent().parent();

                $.post("Student/Delete/", { id: stuid }, function (data) {
                    if (data == "-1") {
                        alert("删除失败");
                    }
                    else {
                        $(tr).remove();
                        alert("删除成功");
                    }

                });
            }
        }
        );
    });
</script>
}


